/* Ekeko - Server based resource loader
 * Copyright (C) 2013 Jorge Luis Zapata
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */
#include "ekeko_common.h"
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
static void _transport_process_request(Eix_Transport *t, Eina_Bool to_read,
		Eina_Bool to_write, void *data)
{
	Ecore_Fd_Handler_Flags flags = 0;
	Ecore_Fd_Handler *handler = data;

	if (to_read)
		flags |= ECORE_FD_READ;
	if (to_write)
		flags |= ECORE_FD_WRITE;
	ecore_main_fd_handler_active_set(handler, flags);
}

static void _transport_process(Ecore_Fd_Handler *handler, Eina_Bool *to_read,
		Eina_Bool *to_write)
{
	*to_read = EINA_FALSE;
	*to_write = EINA_FALSE;

	if (ecore_main_fd_handler_active_get(handler, ECORE_FD_READ))
		*to_read = EINA_TRUE;
	if (ecore_main_fd_handler_active_get(handler, ECORE_FD_WRITE))
		*to_write = EINA_TRUE;
}

static Eina_Bool _server_process(void *data, Ecore_Fd_Handler *handler)
{
	Eix_Server *s = data;
	Eina_Bool to_read;
	Eina_Bool to_write;

	_transport_process(handler, &to_read, &to_write);
	/* call the real process */
	eix_server_process(s, to_read, to_write);
	return EINA_TRUE;
}

static Eina_Bool _client_process(void *data, Ecore_Fd_Handler *handler)
{
	Eix_Client *c = data;
	Eina_Bool to_read;
	Eina_Bool to_write;

	_transport_process(handler, &to_read, &to_write);
	/* call the real process */
	eix_client_process(c, to_read, to_write);
	return EINA_TRUE;
}

static Ecore_Fd_Handler * _transport_loopize(Eix_Transport *t,
		Ecore_Fd_Cb cb, void *data)
{
	Ecore_Fd_Handler *handler;
	Ecore_Fd_Handler_Flags flags = 0;
	Eina_Bool to_read;
	Eina_Bool to_write;
	int fd;

	eix_transport_process_required(t, &to_read, &to_write);
	if (to_read)
		flags |= ECORE_FD_READ;
	if (to_write)
		flags |= ECORE_FD_WRITE;
	/* add the ecore fd callbacks */
	fd = eix_transport_unix_tcp_fd_get(t);
	handler = ecore_main_fd_handler_add(fd, flags, cb, data,
			NULL, NULL);
	/* set the callback whenever a request happens */
	eix_transport_process_request_set(t, _transport_process_request,
			handler);
	return handler;
}
/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/
/* pretty weird the name, but for now we will do this, until the names are
 * a little bit sanitize
 */
void ekeko_common_ecore_unix_udp_server_loopize(Eix_Server *s)
{
	Ecore_Fd_Handler *handler;
	Eix_Transport *t;

	t = eix_server_transport_get(s);
	handler = _transport_loopize(t, _server_process, s);
	eix_server_data_set(s, "_ekeko_ecore_handler", handler);
}

void ekeko_common_ecore_unix_udp_server_unloopize(Eix_Server *s)
{
	Ecore_Fd_Handler *handler;
	handler = eix_server_data_get(s, "_ekeko_ecore_handler");
	ecore_main_fd_handler_del(handler);
}

void ekeko_common_ecore_unix_udp_client_loopize(Eix_Client *c)
{
	Ecore_Fd_Handler *handler;
	Eix_Transport *t;

	t = eix_client_transport_get(c);
	handler = _transport_loopize(t, _client_process, c);
	eix_client_data_set(c, "_ekeko_ecore_handler", handler);
}

void ekeko_common_ecore_unix_udp_client_unloopize(Eix_Client *c)
{
	Ecore_Fd_Handler *handler;
	handler = eix_client_data_get(c, "_ekeko_ecore_handler");
	ecore_main_fd_handler_del(handler);
}
/*============================================================================*
 *                                   API                                      *
 *============================================================================*/

